package com.hospital.service.impl;

import cn.hutool.core.io.resource.InputStreamResource;
import cn.hutool.http.HttpUtil;
import com.github.pagehelper.PageInfo;
import com.hospital.dao.MedicineDao;
import com.hospital.entity.Medicine;
import com.hospital.service.MedicineService;
import com.hospital.vo.ResponseVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 药品接口实现类
 * @author coder_zzm
 * @date 2022/5/8 22:38
 */
@Slf4j
@Service
public class MedicineServiceImpl implements MedicineService {

    @Resource
    private MedicineDao medicineDao;

    /**
     * 获取所有药品信息
     * @param pageNum 页码
     * @param pageSize 每页显示数量
     * @return 药品信息列表
     */
    @Override
    public ResponseVO getAllAndPage(Integer pageNum, Integer pageSize) {
        log.info("业务层获取所有药品信息");
        List<Medicine> allAndPage = medicineDao.getAllAndPage(pageNum, pageSize);

        // 通过 pageHelper 工具类实现自定义分页
        PageInfo<Medicine> pageInfo = new PageInfo<>(allAndPage);
        return ResponseVO.buildSuccessInstance(pageInfo);
    }

    /**
     * 查询药品详细信息
     * @param mId 药品编号
     * @return 药品详细信息
     */
    @Override
    public ResponseVO getMedicineDetail(String mId) {
        log.info("业务层获取单条药品信息");
        Medicine medicine = medicineDao.getMedicineDetail(mId);
        if (medicine != null) {
            return ResponseVO.buildSuccessInstance(medicine);
        }
        return ResponseVO.buildSuccessInstance(ResponseVO.NULL_RESULT);
    }

    /**
     * 根据药品编号删除药品信息
     * @param mId 药品编号
     * @return 受影响行数
     */
    @Override
    public ResponseVO delMedicineGetById(String mId) {
        log.info("业务层删除单条药品信息");
        Integer row = medicineDao.delMedicineGetById(mId);
        if (row > 0) {
            return ResponseVO.buildSuccessInstance(ResponseVO.RESP_SUCCESS, row);
        }
        return ResponseVO.buildSuccessInstance(ResponseVO.DELETE_FAIL, 0);
    }

    /**
     * 添加药品信息
     * @param medicine 药品实体类
     * @return 药品信息是否添加成功
     */
    @Override
    public ResponseVO addMedicine(Medicine medicine) throws IOException {
        log.info("业务层添加药品信息");
        // 文件上传写法
        MultipartFile multipartFile = medicine.getMultipartFile();
        String fileName = System.currentTimeMillis() + "-" + multipartFile.getOriginalFilename();
        InputStreamResource inputStreamResource = new InputStreamResource(multipartFile.getInputStream(), fileName);
        Map<String, Object> map = new HashMap<>();
        map.put("insert", inputStreamResource);
        HttpUtil.post("http://172.20.10.2:280/hospital-pic/", map);
        medicine.setMPicture(fileName);

        Integer result = medicineDao.addMedicine(medicine);
        if (result > 0) {
            return ResponseVO.buildSuccessInstance(ResponseVO.RESP_SUCCESS);
        }
        return ResponseVO.buildSuccessInstance(ResponseVO.UPDATE_FAIL);
    }

    /**
     * 根据药品编号修改药品信息
     * @param medicine 药品实体类
     * @return 药品是否修改成功
     */
    @Override
    public ResponseVO updateMedicine(Medicine medicine) throws IOException {
        log.info("业务层开始修改药品信息");
        log.info(String.valueOf(medicine));
        // 文件上传写法
        MultipartFile multipartFile = medicine.getMultipartFile();
        String fileName = System.currentTimeMillis() + "-" + multipartFile.getOriginalFilename();
        InputStreamResource inputStreamResource = new InputStreamResource(multipartFile.getInputStream(), fileName);
        Map<String, Object> map = new HashMap<>();
        map.put("update", inputStreamResource);
        HttpUtil.post("http://172.20.10.2:280/hospital-pic/", map);
        medicine.setMPicture(fileName);

        Integer result = medicineDao.updateMedicine(medicine);
        if (result > 0) {
            return ResponseVO.buildSuccessInstance(ResponseVO.RESP_SUCCESS);
        }
        return ResponseVO.buildSuccessInstance(ResponseVO.UPDATE_FAIL);
    }
}
